import { showSuccessToast, showFailToast, showConfirmDialog, showDialog } from 'vant';
import BusinessStore from '@/stores/business';
import AddressStore from '@/stores/address';
import CartStore from '@/stores/cart';

// 操作成功提示信息
const success = (options) =>
{
    let message = '';
    let duration = 2000;
    let onClose = () => {};

    if(typeof options === 'object')
    {
        message = options?.message ? options.message : '未知信息';
        duration = typeof options?.duration === 'number' ? options.duration : 2000;
        onClose = typeof options?.onClose === 'function' ? options.onClose : () => {};

    }else if(typeof options === 'string')
    {
        message = options;
    }

    showSuccessToast({
        message,
        duration,
        onClose
    })
}

const fail = (options) =>
{
    let message = '';
    let duration = 2000;
    let onClose = () => {};

    if(typeof options === 'object')
    {
        message = options?.message ? options.message : '未知信息';
        duration = typeof options?.duration === 'number' ? options.duration : 2000;
        onClose = typeof options?.onClose === 'function' ? options.onClose : () => {};

    }else if(typeof options === 'string')
    {
        message = options;
    }

    showFailToast({
        message,
        duration,
        onClose
    })
}

export default {
    install(app)
    {
        // 初始化状态
        const BusinessStoreInit = BusinessStore();
        const AddressStoreInit = AddressStore();
        const CartStoreInit = CartStore();

        // 从pinia中获取数据
        const { business } = storeToRefs(BusinessStoreInit)
        const { address } = storeToRefs(AddressStoreInit)
        const { cart } = storeToRefs(CartStoreInit)

        // 定义全局属性
        app.config.globalProperties.$success = success;
        app.config.globalProperties.$fail = fail;
        app.config.globalProperties.$confirm = showConfirmDialog;
        app.config.globalProperties.$showDialog = showDialog;

        app.config.globalProperties.$business = business;
        app.config.globalProperties.$address = address;
        app.config.globalProperties.$cart = cart;
    }
}